home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5382 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  114 lines

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Bitwise Operators, Help with please!
  5. Date: 12 Feb 1996 11:21:16 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4fo3vcINN7ad@anvil.ugrad.cs.ubc.ca>
  8. References: <4fjaju$i1o_001@pr.mcs.net>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4fjaju$i1o_001@pr.mcs.net>,
  12. Michael D. Perry <mdp@mika-sys.com> wrote:
  13.  >Hello All;
  14.  >
  15.  >I am trying to teach myself 'C' programming from a book which 
  16.  >perhaps is not explaining the Bitwise Operators as well as I 
  17.  >would desire in order to understand them.
  18.  >
  19.  >After going through a few examples the book is having me write a 
  20.  >program that will pack som data and then unpak using bitwise 
  21.  >operators.  I have the packing down OK and can print the bits but 
  22.  >am just not getting the Unpacking.
  23.  >
  24.  >What I want to do is to unpack each 'field' of a type short with 
  25.  >the data as follows:
  26.  >
  27.  >Identification        Job Type    Gender
  28.  >==============        ========    ======
  29.  >bbbbbbbbb        bbbbbb        b
  30.  >
  31.  >and then be able to read the returned bits and conver them back 
  32.  >to their original 'readable' form.
  33.  >
  34.  >Can someone help me with some code so I can at least see how this 
  35.  >would be done and maybe understand it then?
  36.  >
  37.  >What I have so far is listed below and thanks for your 
  38.  >assistance in advance.
  39.  >
  40.  >Mike
  41.  >
  42.  >
  43.  >= = = = = = = = = = 
  44.  >
  45.  >#include <stdio.h>
  46.  >#include <limits.h>
  47.  >
  48.  >short    create_employee_data(int, int, char);
  49.  >void    bit_print(int);
  50.  >short    unpak(int, int);
  51.  >
  52.  >main()
  53.  >{
  54.  >    int    id_no = 255;
  55.  >    int    job_type = 63;
  56.  >    char    gender    = 'F';
  57.  >    short   employee;
  58.  >
  59.  >    employee = create_employee_data(id_no,
  60.  >                    job_type,
  61.  >                    gender);
  62.  >    bit_print(employee);
  63.  >    printf("\n%d", char_unpak(employee, 32766));
  64.  
  65. I don't see char_unpak defined anywhere in your program. There is unpak(),
  66. which takes a different number of arguments. Thus nobody can help you with the
  67. claim that your unpacking is not working.
  68.  
  69. You should probably have a packing/unpacking filter routine for each data type
  70. that you tend to pack and unpack. That is, pack_employee_data() and
  71. unpack_employee_data().
  72.  
  73. By the way, have you heard of bitfields? In a C structure, for integer elements
  74. you can specify how many bits they are to hold, like this:
  75.  
  76. struct foo {
  77.     int x : 3;    /* x is a "tiny signed integer" of three bits    */
  78.     int y : 4;    /* y holds four bits                */
  79. }
  80.  
  81. The field elements may be signed or unsigned. The physical layout depends on
  82. the machine and the compiler you are using---it may or may not be packed, and
  83. you can't depend on your code to be portable if you, say, base code on
  84. the assumption that x and y belong to the same word in a particular order. 
  85.  
  86.  >    return 0;
  87.  >}
  88.  >
  89.  >short    create_employee_data(int id_no,
  90.  >                int job_type,
  91.  >                char gender)
  92.  >{
  93.  >    short employee    =    0;
  94.  >
  95.  >    employee |= (gender == 'm' || gender == 'M') ? 0 : 1;
  96.  
  97.     This is OK so far!
  98.  
  99.  >    employee |= (job_type << 1);
  100.  
  101. This is bad! job_type could have more bits than you bargained for, thus setting
  102. too many bits. but let's assume that job_type is already masked by the caller
  103. to the right number of bits to match the size of the field you are encoding...
  104.  
  105.  >    employee |= (id_no << 7);
  106.  
  107. Ditto.
  108.  
  109.  >    return employee;
  110.  >}
  111.  >
  112. -- 
  113.  
  114.